#import "SFUPersistentEventCollection.h"

/**
 * An SFUPersistentEventCollection is an object representing a
 * persistent collection of SFU events.
 *
 * @author      James Lin
 * @version     0.6.2
 * @since       0.3.0
 */
@implementation SFUPersistentEventCollection

/**
 * Initialize the SFUPersistentEventCollection as an empty collection.
 * 
 * @param fileName  the name of the file in which to store this collection
 * @return          the initialized empty SFUPersistentEventCollection
 */
- (id)initWithEmptyFile:(NSString*)fileName {
	return (self = [super initWithEmptyFile:fileName 
						  usingSubdirectory:EVENT_SUBDIRECTORY]);
}

/**
 * Initialize the SFUPersistentEventCollection with existing persistent data.
 * 
 * If the file at the path is non-existent, it is created.
 *
 * @precondition    the file at the path is valid or non-existent
 * @param fileName  the path from which to load or store this collection
 * @return          the initialized SFUPersistentEventCollection
 */
- (id)initWithFile:(NSString*)fileName {
	return (self = [super initWithFile:fileName 
					 usingSubdirectory:EVENT_SUBDIRECTORY]);
}

/**
 * Initialize the SFUPersistentEventCollection with an array of SFU events.
 * 
 * @precondition    the events are all valid and unique
 * @param events    the array of events with which to initialize
 * @param fileName  the path at which to store this collection
 * @return          the initialized SFUPersistentEventCollection with specified events
 */
- (id)initWithEvents:(NSArray*)events 
				file:(NSString*)fileName {
	return (self = [super initWithDataTypes:events 
									   file:fileName 
						  usingSubdirectory:EVENT_SUBDIRECTORY]);
}

/**
 * Retrieve write-permissible absolute path for storing this collection.
 *
 * Asks the iOS for the application's unique Documents directory.
 *
 * @param fileName  the name of the file in which to store this collection
 * @return          the absolute path at which this collection is stored
 */
+ (NSString*)getPathWithFileName:(NSString*)fileName {
	return [FileUtilities getPathWithFileName:fileName 
							usingSubdirectory:EVENT_SUBDIRECTORY];
}

/**
 * Retrieve the unique key used for archiving.
 *
 * @return  the key used for keyed archiving
 */
- (NSString*)getArchiveKey {
	return SFUEVENT_ARCHIVE_KEY;
}

/**
 * Add an SFU event to the collection.
 * 
 * @precondition  the event is unique in the collection
 * @param event   the event to add
 */
- (void)addEvent:(id<PersistentDataType>)event {
	SFUEvent* updatedEvent = (SFUEvent*)event;
	updatedEvent.isBookmarked = YES;
	[super addDataType:updatedEvent];
}

/**
 * Add an array of SFU events to the collection.
 *
 * @precondition  the events are all unique in the collection
 * @param events  the array of events to add
 */
- (void)addEvents:(NSArray*)events {
	[super addDataTypes:events];
}

/**
 * Add another SFU event collection to this collection.
 *
 * @param eventCollection  the event collection to add to this collection
 */
- (void)addEventCollection:(PersistentDataCollection*)eventCollection {
	[super addDataCollection:eventCollection];
}

/**
 * Get the events in the given list of events that are in 
 * this collection.
 *
 * @param dataTypes  the list of events of which to check intersection
 * @return           the events in the given list that are in this collection
 */
- (NSArray*)intersectEvents:(NSArray*)events {
	return [super intersectDataTypes:events];
}

/**
 * Determine whether the given event is in the collection.
 *
 * @param event  the event of which to check existence
 * @return       YES if the event is in the collection;
 *               NO otherwise.
 */
- (BOOL)containsEvent:(id<PersistentDataType>)event {
	return [super containsDataType:event];
}

/**
 * Retrieve all events in the collection.
 *
 * @return  an array of all events in the collection
 */
- (NSArray*)getEvents {
	return [super getDataTypes];
}

/**
 * Find all SFU events whose heading or category contains
 * any or all keywords in the given space-delimited keyword string.
 *
 * If matching by any keyword in the keyword list, an additional sort
 * will be performed on the resulting list after the completed query.
 * The resulting list of events will be in non-increasing order of 
 * the number of keywords matched.
 * 
 * @precondition    keyword string is not nil
 * @param keyword   the case-insensitive search keyword string
 * @param matchAll  YES if all keywords in the keyword list must match;
 *                  NO if any keyword can match.
 * @return          an array of SFU events with suitable matches
 */
- (NSArray*)findEventsWithKeywordString:(NSString*)keyword 
					   matchAllKeywords:(BOOL)matchAll {
	return [super findDataTypesWithKeywordString:keyword 
								matchAllKeywords:matchAll];
}

/**
 * Find all SFU events whose heading or category contains
 * any or all of the keywords in the given keyword list.
 *
 * If matching by any keyword in the keyword list, an additional sort
 * will be performed on the resulting list after the completed query.
 * The resulting list of events will be in non-increasing order of 
 * the number of keywords matched.
 *
 * @precondition    keywords are not nil
 * @param keywords  the list of case-insensitive search keywords
 * @param matchAll  YES if all keywords in the keyword list must match;
 *                  NO if any keyword can match.
 * @return          an array of SFU events with suitable matches
 */
- (NSArray*)findEventsWithKeywords:(NSArray*)keywords 
				  matchAllKeywords:(BOOL)matchAll {
	return [super findDataTypesWithKeywords:keywords 
						   matchAllKeywords:matchAll];
}

/**
 * Retrieve the event with the given key.
 *
 * @param key  the key of the event to retrieve
 */
- (id<PersistentDataType>)getEventWithKey:(NSString*)key {
	return [super getDataTypeWithKey:key];
}

/**
 * Remove an SFU event from the collection.
 *
 * @param event  the event to remove
 */
- (void)removeEvent:(id<PersistentDataType>)event {
	[super removeDataType:event];
}

@end
